package cn.jf.common.system.util;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 *
 * @Return
 * @Author jf
 * @Date 2019年7月26日11:05:38
 **/
public class OrderUtil {
    public static List<OrderItem> format(HttpServletRequest request){
        String column = request.getParameter("column");
        if(StrUtil.isNotBlank(column)) {
            column = StringUtils.camelToUnderline(column);
            String order = request.getParameter("order");
            if(StrUtil.isNotBlank(order)){
                order = order.toLowerCase();
                List<OrderItem> list = new ArrayList<>();
                OrderItem orderItem = new OrderItem();
                orderItem.setColumn(column);
                orderItem.setAsc(order.equals("asc"));
                list.add(orderItem);
                return list;
            }else{
                return null;
            }

        }else {
            return null;
        }
    }
    public static List<OrderItem> formatBiz(HttpServletRequest request,String columnBiz){
        String column ="";
        if(request == null){
            column = columnBiz;
        }else {
            request.getParameter("column");
        }
        if(StrUtil.isNotBlank(column)) {
            if("createTime".equals(column)){
                column = columnBiz;
            }
            column = StringUtils.camelToUnderline(column);
            String order ="";
            if(null != request){

                order = request.getParameter("order");
            }
            if(StrUtil.isNotBlank(order)){
                order = order.toLowerCase();
                List<OrderItem> list = new ArrayList<>();
                OrderItem orderItem = new OrderItem();
                orderItem.setColumn(column);
                orderItem.setAsc(order.equals("asc"));
                list.add(orderItem);
                return list;
            }else{
                return null;
            }

        }else {
            return null;
        }
    }
}
